home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / C Libraries / StringTools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-04  |  3.2 KB  |  113 lines  |  [TEXT/KAHL]

  1. #include <StringTools.h>
  2. #include <stdarg.h>            // for va_xxx
  3. #include <string.h>            // for memcmp
  4.  
  5. /*------------------------------------------------------------------------------*/
  6.  
  7.     void    CopyString (Str255 String1, Str255 String2)
  8.     /* copy the entire contents of String1 into String2 */
  9.     {
  10.         BlockMove (String1, String2, String1[0]+1);
  11.     }
  12.  
  13. /*------------------------------------------------------------------------------*/
  14.  
  15.     StringPtr    String2String (StringPtr InString)
  16.     /* make a copy of the passed string */
  17.     {
  18.         StringPtr    NewString = NULL;
  19.         
  20.         if (InString)
  21.           {
  22.             NewString = (StringPtr)NewPtr(*InString + 1);
  23.             if (NewString)
  24.               BlockMove (InString, NewString, *InString+1);
  25.           }
  26.         return NewString;
  27.     }
  28.  
  29. /*------------------------------------------------------------------------------*/
  30.  
  31.     void PStrCpy (StringPtr Source, StringPtr Dest, short numToCopy)
  32.     /* Given two pascal strings, this routine replaces the contents of */
  33.     /* the second string with numToCopy characters of the first string */
  34.     {
  35.         BlockMove (Source, Dest, numToCopy);
  36.         Dest[0] = numToCopy;
  37.     }
  38.  
  39. /*------------------------------------------------------------------------------*/
  40.  
  41.     short PStrLen (StringPtr TheString)
  42.     {
  43.         if (TheString)
  44.           return *TheString;
  45.         else
  46.           return 0;
  47.     }
  48.  
  49. /*------------------------------------------------------------------------------*/
  50.  
  51.     short PStrCmp (StringPtr String1, StringPtr String2)
  52.     /* if String1 is > String2, return a positive number; if String1 is equal to */
  53.     /* String2, return 0; if String1 is < String2, return a negative number */
  54.     {
  55.         short    Result;
  56.         
  57.         // first handle either or both of the inputs being NULL
  58.         if ((!String1) && (String2)) return -1;
  59.         if ((!String2) && (String1)) return 1;
  60.         if (!String2 && !String1)    return 0;
  61.           
  62.         // we know both strings are valid; use memcmp to compare them
  63.         Result = memcmp (String1+1, String2+1, (*String1 > *String2) ? *String2 : *String1);
  64.         if ((Result == 0) && (*String1 != *String2))    // if strings are Δ lengths,
  65.           return ((*String1 > *String2) ? 1 : -1);        // modify result based on length
  66.         else                                            // otherwise
  67.           return Result;                                // return the result unchanged
  68.     }
  69.  
  70.     
  71. /*------------------------------------------------------------------------------*/
  72.  
  73.     Boolean PStringCmp (register StringPtr String1, register StringPtr String2)
  74.     {
  75.         short    i, max = String1[0];
  76.         
  77.         if (String1[0] != String2[0])
  78.           return FALSE;
  79.           
  80.         for (i = 1;i <= max;i++)
  81.           if (String1[i] != String2[i])
  82.             return FALSE;
  83.             
  84.         return TRUE;
  85.     }
  86.  
  87. /*------------------------------------------------------------------------------*/
  88.  
  89.     StringPtr    PStrCat (short maxLen, Str255 Dest, short numArgs,...)
  90.     /* concatenate a variable number of StringPtr's and store the result in */
  91.     /* Dest; Dest will receive no more than maxLen characters, no matter how many */
  92.     /* StringPtr's are passed in */
  93.     {
  94.         short        i = 1, j = 1;
  95.         va_list        ap;
  96.         StringPtr    STemp = NULL;
  97.         short        argsLeft = numArgs;
  98.         
  99.         va_start (ap, numArgs);
  100.         while (numArgs -- > 0)
  101.           {
  102.               STemp = (StringPtr)(va_arg (ap, Ptr));    // get the nth argument
  103.               
  104.               // copy characters from the source string to the Dest string
  105.               for (j = 1; j <= *STemp; j++, i++)
  106.                 if (i <= maxLen)
  107.                   Dest[i] = STemp[j];
  108.           }        
  109.         va_end (ap);
  110.         Dest[0] = i-1;
  111.     }
  112.  
  113.